package cn.gcyl.module.biz.service.conferenceregistrationinformation;

import cn.gcyl.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gcyl.framework.security.core.LoginUser;
import cn.gcyl.module.biz.dal.dataobject.SysPayOrderDO;
import cn.gcyl.module.biz.dal.dataobject.conferenceregistrationinformation.ConferenceUserRegistrationInformationPO;
import cn.gcyl.module.biz.dal.mysql.SysPayOrderMapper;
import cn.gcyl.module.biz.dal.mysql.meetingplan.MeetingPlanMapper;
import cn.gcyl.module.biz.dal.mysql.specialmeeting.SpecialMeetingMapper;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.gcyl.module.biz.controller.admin.conferenceregistrationinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.conferenceregistrationinformation.ConferenceRegistrationInformationDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.conferenceregistrationinformation.ConferenceRegistrationInformationMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.CONFERENCE_REGISTRATION_INFORMATION_NOT_EXISTS;

/**
 * 会议报名信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ConferenceRegistrationInformationServiceImpl implements ConferenceRegistrationInformationService {

    @Resource
    private ConferenceRegistrationInformationMapper conferenceRegistrationInformationMapper;
    @Resource
    private SpecialMeetingMapper specialMeetingMapper;
    @Resource
    private MeetingPlanMapper meetingPlanMapper;
    @Resource
    private SysPayOrderMapper sysPayOrderMapper;

    @Override
    public Long createConferenceRegistrationInformation(ConferenceRegistrationInformationSaveReqVO createReqVO) {
        // 插入
        ConferenceRegistrationInformationDO conferenceRegistrationInformation = BeanUtils.toBean(createReqVO, ConferenceRegistrationInformationDO.class);
        conferenceRegistrationInformationMapper.insert(conferenceRegistrationInformation);

        // 返回
        return conferenceRegistrationInformation.getId();
    }

    @Override
    public void updateConferenceRegistrationInformation(ConferenceRegistrationInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateConferenceRegistrationInformationExists(updateReqVO.getId());
        // 更新
        ConferenceRegistrationInformationDO updateObj = BeanUtils.toBean(updateReqVO, ConferenceRegistrationInformationDO.class);
        conferenceRegistrationInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteConferenceRegistrationInformation(Long id) {
        // 校验存在
        validateConferenceRegistrationInformationExists(id);
        // 删除
        conferenceRegistrationInformationMapper.deleteById(id);
    }

    @Override
    public void deleteConferenceRegistrationInformationListByIds(List<Long> ids) {
        // 删除
        conferenceRegistrationInformationMapper.deleteByIds(ids);
    }


    private void validateConferenceRegistrationInformationExists(Long id) {
        if (conferenceRegistrationInformationMapper.selectById(id) == null) {
            throw exception(CONFERENCE_REGISTRATION_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public ConferenceRegistrationInformationDO getConferenceRegistrationInformation(Long id) {
        return conferenceRegistrationInformationMapper.selectById(id);
    }

    @Override
    public PageResult<ConferenceRegistrationInformationDO> getConferenceRegistrationInformationPage(ConferenceRegistrationInformationPageReqVO pageReqVO) {
        return conferenceRegistrationInformationMapper.selectPage(pageReqVO);
    }

    @Override
    public ConferenceUserRegistrationInformationPO getMeetingInfo(Long id) {
        return conferenceRegistrationInformationMapper.getMeetingInfo(id);
    }

    @Override
    public PageResult<ConferenceUserRegistrationInformationPO> getMeetingListPage(ConferenceMeetingInformationPageReqVO pageReqVO) {
        LoginUser loginUser = getLoginUser();
        // 1. 构建MyBatis-Plus分页对象
        Page<ConferenceUserRegistrationInformationPO> page = new Page<>(
                pageReqVO.getPageNo(),
                pageReqVO.getPageSize()
        );
        // 2. 执行分页查询
        IPage<ConferenceUserRegistrationInformationPO> pageResult =
                conferenceRegistrationInformationMapper.getMeetingListPage(page, pageReqVO);

        // 3. 转换为自定义PageResult
        return new PageResult<>(
                pageResult.getRecords(),
                pageResult.getTotal()
        );
    }

    @Override
    public void createConferenceRegistrationInformationDO(ConferenceRegistrationInformationDO registrationInformationDO) {
        conferenceRegistrationInformationMapper.insert(registrationInformationDO);
    }

    @Override
    public Boolean getConferenceRegistrationInformationByOrderNo(String merchantOrderId, SysPayOrderDO sysPayOrderDO) {
        SysPayOrderDO payOrderDO = sysPayOrderMapper.selectById(sysPayOrderDO.getId());
        log.info("回调内容：···{}", payOrderDO);
        if (payOrderDO.getStatus() == 10) {
            ConferenceRegistrationInformationDO informationDO = conferenceRegistrationInformationMapper.selectOne(ConferenceRegistrationInformationDO::getOrderNo, merchantOrderId);
            informationDO.setPayStatus(1);
            informationDO.setIsFree(1);
            conferenceRegistrationInformationMapper.updateById(informationDO);
        }
        return true;
    }

    @Override
    public ConferenceRegistrationInformationDO getMeetingInfoByUserId(Long meetingId, Long loginUserId) {
        return conferenceRegistrationInformationMapper.selectOne(new LambdaQueryWrapperX<ConferenceRegistrationInformationDO>()
                .eqIfPresent(ConferenceRegistrationInformationDO::getMeetingId, meetingId)
                .eqIfPresent(ConferenceRegistrationInformationDO::getUserId, loginUserId)
                .eqIfPresent(ConferenceRegistrationInformationDO::getPayStatus, 1)
        );
    }
//    @Override
//    public ConferenceUserRegistrationInformationPO getMeetingInfo(Long id) {
//        List<ConferenceUserRegistrationInformationPO> results = conferenceRegistrationInformationMapper.getMeetingInfo(id);
//        return results.isEmpty() ? null : results.get(0);
//    }

}